# vim: ai ts=2 sts=2 et sw=2

import utils

class TileMap:
  def __init__(self):
    self.name = ""
    self.width = 10
    self.height = 10
    self.tileWidth = 32
    self.tileHeight = 32
    self.tileData = {}
    self.bottomLayer = {}
    self.middleLayer = {}
    self.topLayer = {}

  def load_tileset(self, fileName, tileWidth, tileHeight):
    self.tileset = utils.load_image(fileName)
    self.tilesetWidth = self.tileset.get_width() / tileWidth
    self.tilesetHeight = self.tileset.get_height() / tileHeight
    self.tileWidth = tileWidth
    self.tileHeight = tileHeight

  def load_map(self, fileName):
    file = open(fileName)
    
    x = 0
    y = 0
    for line in file:
      line = line.strip()
      if len(line) > 1 and line[0] != "#":
        x = 0
        for data in line.split(":"):
          self.tileData[x, y] = int(data.split(",")[0])
          self.bottomLayer[x, y] = int(data.split(",")[0])
          self.middleLayer[x, y] = int(data.split(",")[1])
          self.topLayer[x, y] = int(data.split(",")[2])
          x += 1
        y += 1

  def load_map2(self, fileName):
    pass
      
class TileMapView:
  def __init__(self, tileMap, left, top, width, height):
    self.left = left
    self.top = top
    self.width = width
    self.height = height
    self.tileMap = tileMap
    
  def draw(self, screen):
    screen.fill((255, 255, 255), (self.left, self.top, self.width * self.tileMap.tileWidth, self.height * self.tileMap.tileHeight))

    for x in range(0, self.width):
      for y in range(0, self.height):
        
        # make sure we don't go off the edge of the map when drawing...
        if x in range(0, self.tileMap.width) and y in range(0, self.tileMap.height):

          # if the bottom layer tile is set, draw it
          if self.tileMap.bottomLayer != -1:
            clipLeft = self.tileMap.tileWidth * (self.tileMap.bottomLayer[x, y] % self.tileMap.tilesetWidth)
            clipTop = self.tileMap.tileHeight * (self.tileMap.bottomLayer[x, y] / self.tileMap.tilesetWidth)
            screen.blit(self.tileMap.tileset, (x * self.tileMap.tileWidth + self.left, y * self.tileMap.tileHeight + self.top),
                        (clipLeft, clipTop, self.tileMap.tileWidth, self.tileMap.tileHeight))
          # if the middle layer tile is set, draw it
          if self.tileMap.middleLayer != -1:
            clipLeft = self.tileMap.tileWidth * (self.tileMap.middleLayer[x, y] % self.tileMap.tilesetWidth)
            clipTop = self.tileMap.tileHeight * (self.tileMap.middleLayer[x, y] / self.tileMap.tilesetWidth)
            screen.blit(self.tileMap.tileset, (x * self.tileMap.tileWidth + self.left, y * self.tileMap.tileHeight + self.top),
                        (clipLeft, clipTop, self.tileMap.tileWidth, self.tileMap.tileHeight))
          # if the top layer tile is set, draw it
          if self.tileMap.topLayer != -1:
            clipLeft = self.tileMap.tileWidth * (self.tileMap.topLayer[x, y] % self.tileMap.tilesetWidth)
            clipTop = self.tileMap.tileHeight * (self.tileMap.topLayer[x, y] / self.tileMap.tilesetWidth)
            screen.blit(self.tileMap.tileset, (x * self.tileMap.tileWidth + self.left, y * self.tileMap.tileHeight + self.top),
                        (clipLeft, clipTop, self.tileMap.tileWidth, self.tileMap.tileHeight))

  
